<?php

namespace app\admin\controller;

use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemCron;
use think\exception\HttpResponseException;
/**
 * 定时任务配置
 * @class Cron
 * @package app\admin\controller
 */
class Cron extends Controller
{
    /**
     * 下载器配置
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        SystemCron::mQuery()->layTable(function () {
            $this->title = '定时任务';
        }, static function (QueryHelper $query) {
        });
    }

    /**
     * 列表数据处理
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(array &$data)
    {
        // foreach ($data as &$vo) try {
        //   $vo['type'] == 1 ? $vo['type'] = 'QB' : $vo['type'] = 'TR';
        //   $vo['is_skip_hash'] == 1 ? $vo['is_skip_hash'] = '跳过' : $vo['is_skip_hash'] = '不跳过';
        //   $vo['is_paused'] == 1 ? $vo['is_paused'] = '是' : $vo['is_paused'] = '否';
        //   $vo['reseed_sites_name'] = '';
        
        //   $siteIds = $array = explode(',', $vo['reseed_sites']);
        //   $sitesQuery = SystemSites::mQuery()->db();
        //   $siteNames = $sitesQuery->whereIn('id', $siteIds)->column('site_name');
        //   $vo['reseed_sites_name'] = implode(', ', $siteNames);
        // } catch (\Exception $exception) {
        // }
    }

    /**
     * 删除下载器
     * @auth true
     */
    public function start()
    {
      $title = "123";
      $command = "cron start";
      $later = 0;
      $data = ['id'=>1];
      $rscript = 1; 
      $loops = 999;
      \think\Console::starting(function (Console $console) {
        $console->addCommand(\app\store\command\MemberRun::class);
      });
      sysqueue($title, $command, $later=0, $data, $rscript=0, $loops=0);
    }

    /**
     * 编辑下载器
     * @auth true
     */
    public function edit()
    {
        SystemCron::mForm('edit');
    }
    /**
     * 添加下载器
     * @auth true
     */
    public function add()
    {
        SystemCron::mForm('add');
    }
        /**
     * 删除所有下载器
     * @auth true
     */
    public function clear()
    {
        try {
            SystemCron::mQuery()->empty();
            sysoplog('下载器管理', '成功删除所有下载器');
            $this->success('成功删除所有下载器！');
        } catch (HttpResponseException $exception) {
            throw $exception;
        }
    }
}
